package com.novoda.runbuddy.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.location.Location;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextSwitcher;
import android.widget.TextView;

import com.google.android.maps.MapView;
import com.novoda.runbuddy.activity.RouteRunner;
import com.novoda.runbuddy.activity.stopwatch.RemoteStopClockProxy;
import com.novoda.runbuddy.activity.stopwatch.StopwatchTools;
import com.novoda.runbuddy.model.Route;
import com.novoda.runbuddy.provider.ActivityID;
import com.novoda.runbuddy.provider.RunBuddyCP;
import com.novoda.runbuddy.provider.RunBuddyCPTest;
import com.novoda.runbuddy.service.IRemoteStopClockService;
import com.novoda.runbuddy.view.RouteRunnerView;
import com.novoda.runbuddy.view.StopwatchView;
import com.novoda.runbuddy.view.TargetTimeView;

public class RouteRunnerControllerImpl implements IRouteRunnerController{

	private RouteRunnerView				routeRunnerView;
	private TargetTimeView				stopwatchTargetView;
	protected IRemoteStopClockService	messageService;
	private boolean						targetIsSet	= false;
	private String						targetTime	= "00:00:00.00";
	private RouteRunner	context;

	// public long startTime = 0L;

	public RouteRunnerControllerImpl(RouteRunner routeRunner) {
		this.context = routeRunner;
	}

	public void createRouteRunnerView(ListActivity routeRunner, List<Route> allRoutesAroundCurrentArea, Button startPreviousRoute) {
		routeRunnerView = new RouteRunnerView(routeRunner, this, allRoutesAroundCurrentArea, startPreviousRoute);
	}
	
	public void createStopClockView(Context activity, TextSwitcher stopClockTextSwitcher, Button startButton) {
		//here because the interface is extended.
	}

	public void createwatchClockTargetView(Context context, CheckBox checkBox, TextView hours, TextView minutes, TextView seconds,
			TextView milliseconds, Button hoursUp, Button hoursDown, TextView hoursSep, Button minutesUp, Button minutesDown,
			TextView minutesSep, Button secsUp, Button secsDown, TextView secondsSep, Button millisecsUp, Button millisecsDown) {

		stopwatchTargetView = new TargetTimeView(context, checkBox, hours, minutes, seconds, milliseconds, hoursUp, hoursDown,
				hoursSep, minutesUp, minutesDown, minutesSep, secsUp, secsDown, secondsSep, millisecsUp, millisecsDown, this);

		stopwatchTargetView.hideSetTargetTime();
	}

	public String	elapsedTime	= "0";

	public void start() {

	}

	public void stop() {
	}

	public boolean targetIsSet() {
		return this.targetIsSet;
	}

	public String getTargetTime() {
		return this.targetTime;
	}

	public void setTargetTime(String targetTime) {
		this.targetTime = targetTime;
	}

	public void setTargetCentiSecs(Integer centiSecs) {
		this.targetTime = StopwatchTools.setCentiSecs(centiSecs, this.targetTime);
	}

	public void setTargetSecs(Integer secs) {
		this.targetTime = StopwatchTools.setSecs(secs, this.targetTime);
	}

	public void setTargetMins(Integer mins) {
		this.targetTime = StopwatchTools.setMins(mins, this.targetTime);
	}

	public void setTargetHours(Integer hours) {
		this.targetTime = StopwatchTools.setHours(hours, this.targetTime);
	}

	private boolean isTargetTimeReached(String elapsedTime) {
		int stopClock = StopwatchTools.compare(this.targetTime, elapsedTime);

		return stopClock == StopwatchTools.EQUAL || stopClock == StopwatchTools.GREATER_THAN;
	}

	public void turnOffASetTargetTime() {
		this.targetIsSet = false;
	}

	public void turnOnASetTargetTime() {
		this.targetIsSet = true;
	}

	public void onResume() {

	}

	public Handler getUpdateHandler() {
		//Added via the extended inteface
		return null;
	}

	public List<Route> getRoutesAroundCurrentLocation() {
		Route route;
		Vector<Location> locations = new Vector<Location>();
		ArrayList<Route> collectedRoutes = new ArrayList<Route>();

		String[] cols_names = new String[] { 
				RunBuddyCP.Routes._ID, 
				RunBuddyCP.Routes.SERVER_ID,
				RunBuddyCP.Routes.NAME,
				RunBuddyCP.Routes.CREATED_DATE,
				RunBuddyCP.Routes.MODIFIED_DATE 
				};
		
		Cursor cur = context.getContentResolver().query(RunBuddyCP.Routes.CONTENT_URI, cols_names, null, null, null);	
		Log.d("RouteController", "Got the cursor");

		while (cur.moveToNext()) {			
			
			route = new Route(
					cur.getInt(0),
					cur.getInt(1),
					cur.getString(2),
					locations,
					cur.getLong(3),
					cur.getLong(4));
		
			Log.d("RouteController", "Route =" + route.name);
			collectedRoutes.add(route);
		}

		cur.close();

		return collectedRoutes;
	}

	public void refreshListOfRoutes() {
		routeRunnerView.refreshListOfRoutes();
	}


}
